// components/ordercard/ordercard.js
const http = require('../../utils/http')
const api = require('../../utils/api')
const util = require('../../utils/util')
const ui = require('../../utils/ui')

// 0待支付 1支付成功未消费 2消费中(点击了到店开始) 3已完成
// 全部订单 1234
// 进行中 23

// 待支付 1
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    iid: {type: String},
    oid: {type: String},
    status: {type: Number, value: 3},
    url: {type: String},
    name: {type: String},
    desc: {type: String},
    price: {type: Number},
    memberPrice: {type: Number},
    duration: {type: Number},
    begin: {type: String},
    end: {type: String},
    fillBegin: {type: String},
    fillEnd: {type: String},
    timeout: {type: Number, value: 1800}
  },

  /**
   * 组件的初始数据
   */
  data: {
    _url: '',
    _countdownStr: ''
  },

  lifetimes: {
    attached: function () {
      let urls = this.data.url.split(',')
      console.log("this.data.url", this.data.url, urls)
      if (urls.length > 0) {
        this.setData({
          _url: urls[0]
        })
      }
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    canStart() {
      http.request({
        url: api.canStart,
        data: {
          tradeId: this.data.oid
        }
      }).then(res=> {
        console.log(res)
        let data = res.data.result
        if (data.startState) {
          if (data.code == 2) {
            this.showOrderDialog()
          } 
          else {
            this.gameStart()
          }
        }
        else {
          ui.showToast({
            title: data.msg,
          })
        }
      })
    },
    gameStart() {
      http.request({
        url: api.gameStart,
        method: "POST",
        data: {
          memberId: getApp().globalData.memberId,
          tradeId: this.data.oid
        }
      }).then(res=> {
        let data = res.data.result
        if (!data.startState) {
          ui.showToast({
            title: data.msg,
          })
        }
        else {
          ui.showToast({
            title: data.msg,
          })
          this.triggerEvent('triggerRefresh')
        }
      })
    },
    showOrderDialog() {
      this.triggerEvent('triggerComfirmDialog', this.data.oid);
    },
    wifiInfo() {
      http.request({
        url: api.storeInfo,
        data: {
          tradeId: this.data.oid,
        }
      }).then(res=>{
        console.log(res)
        let data = res.data.result
        this.triggerEvent('triggerWifiDialog', {wifi: data.wifi, password: data.password});
      })
    },
    openLock() {
      http.request({
        url: api.openLock,
        data: {
          tradeId: this.data.oid,
        }
      }).then(res=>{
        console.log(res)
        ui.showToast({
          icon: res.data.result?'success':'error',
          title: res.data.result?'开门成功':'开门失败',
        })
      })
    },
    renewal() {
      let params = {
        "itemId": this.data.iid,
        "tradeId": this.data.oid,
        "startDate": this.data.fillBegin,
        "endDate": this.data.fillEnd,
      }
      wx.navigateTo({
        url: '/pages/renewal/renewal'+util.paramsWrap(params),
      })
    },
    toOrderDetail() {
      let params = {
        "isRenewal": false,
        "oid": this.data.oid,
        "name": this.data.name,
        "desc": this.data.desc,
        "url": encodeURIComponent(this.data.url),
        "time": this.data.begin+" 至 "+this.data.end, 
        "duration": this.data.duration, 
        "price": this.data.price,
        "memberPrice": this.data.memberPrice
      }      
      wx.navigateTo({
        url: '/pages/order_detail/order_detail'+util.paramsWrap(params),
      })
    },
    cancelOrder() {
      console.log("cancel")
      http.request({
        url: api.cancel,
        method: "POST",
        data: {
          tradeId: this.data.oid,
        }
      }).then(res=>{
        console.log(res)
        ui.showToast({
          title: res.data.result?"取消成功":"取消失败"
        })
        this.triggerEvent('triggerRefresh')
      })
    },
    tick (delta) {
      if (this.data.timeout > 0) {
        this.setData({
          _countdownStr: this.sToMs(this.data.timeout -= delta)
        })
      }
      if (this.data.timeout == 0 && this.data.status == 0) {
        this.triggerEvent('triggerRefresh')
      }
    },
    sToMs(s){
      var h;
      h = Math.floor(s/60);
      s = s%60;
      h += '';
      s += '';
      h = (h.length==1)?'0'+h:h;
      s = (s.length==1)?'0'+s:s;
      return h+':'+s;
    },
    prefixZero(num, n) {// 前补零
      return (Array(n).join(0) + num).slice(-n);
    }
  }
})
